﻿{include file="base/_meta" /}
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">标题:</label>
                        <div class="layui-input-inline">
                            <input name="title" class="layui-input" placeholder="标题"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">分类:</label>
                        <div class="layui-input-inline">
                            <div id="cate" class="xm-select-cate"></div>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">状&emsp;态:</label>
                        <div class="layui-input-inline">
                            <select name="show">
                                <option value="">选择状态</option>
                                <option value="10">显示</option>
                                <option value="20">隐藏</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">置顶:</label>
                        <div class="layui-input-inline">
                            <select name="top">
                                <option value=""></option>
                                <option value="10">置顶</option>
                                <option value="20">不置顶</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline layui-btn-group">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="userTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button class="layui-btn icon-btn" onclick="window.location.reload();">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>&nbsp;
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="userTable" lay-filter="userTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="userTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="TbShow">
    <input type="checkbox" lay-filter="TbShowCk" value="{{d.id}}" lay-skin="switch"
           lay-text="显示|隐藏" {{d.show==10?'checked':''}} style="display: none;"/>
    <p style="display: none;">{{d.show==10?'显示':'隐藏'}}</p>
</script>

<script type="text/html" id="TbTop">
    <input type="checkbox" lay-filter="TbTopCk" value="{{d.id}}" lay-skin="switch"
           lay-text="置顶|不置顶" {{d.top==10?'checked':''}} style="display: none;"/>
    <p style="display: none;">{{d.top==10?'置顶':'不置顶'}}</p>
</script>


<!-- js部分 -->
{include file="base/_script" /}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'xmSelect','index'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var xmSelect = layui.xmSelect;
        var index = layui.index;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#userTable',
            url: "{:url('article/article')}",
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {field: 'id', title: 'ID'},
                {field: 'title', title: '名称'},
                {field: 'sort', title: '排序', sort: true},
                {field: 'state', title: '状态', templet: '#TbShow', sort: true, width: 100},
                {field: 'state', title: '置顶', templet: '#TbTop', sort: true, width: 100},
                {title: '操作', toolbar: '#userTbBar', align: 'center', minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(userTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(userTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                index.openTab({
                    title: '添加内容',
                    url: "{:url('article/form')}?id="+obj.data.id,
                    end: function() {
                        insTb.reload({where: '', page: {curr: 1}});
                    }
                });
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(userTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                index.openTab({
                    title: '添加内容',
                    url: "{:url('article/form')}",
                    end: function() {
                        insTb.reload({where: '', page: {curr: 1}});
                    }
                });
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('userTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.get("{:url('article/article_del')}", {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        /* 修改状态 */
        form.on('switch(TbShowCk)', function (obj) {
            var loadIndex = layer.load(2);
            $.get("{:url('article/article_edit_save')}", {
                id: obj.elem.value,
                show: obj.elem.checked ? 10 : 20
            }, function (res) {
                layer.close(loadIndex);
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });
        form.on('switch(TbTopCk)', function (obj) {
            var loadIndex = layer.load(2);
            $.get("{:url('article/article_edit_save')}", {
                id: obj.elem.value,
                top: obj.elem.checked ? 10 : 20
            }, function (res) {
                layer.close(loadIndex);
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

        /* 分类 */
        $.post("{:url('article/article_cate_tree')}",function (ret) {
            var cateXmSelect = xmSelect.render({
                el: '#cate',
                height: '250px',
                name:"article_cate_id",
                data: ret.data,
                initValue: [],
                model: {
                    label: {type: 'text'},
                },
                prop: {
                    name: 'name',
                    value: 'id'
                },
                tree: {
                    show: true,
                    indent: 15,
                    strict: false,
                    expandedKeys: true
                }
            })
        })
    });
</script>
</body>
</html>
